package com.yanqu.road.server.cmd.activity.tongbing;

import com.yanqu.road.entity.activity.tongbing.config.TongBingConfig;
import com.yanqu.road.entity.enums.eTongBingStage;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.pb.activity.TongBingProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.tongbing.TongBingMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = GameProtocol.S_TONG_BING_JOIN, desc = "")
public class TongBingJoinCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        TongBingConfig config = TongBingMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        // 新版活动无法手动报名
        if (UnionActivityGroupHelper.needGroup(config.getActivityInfo())) {
            ret = GameErrorCode.E_TONG_BING_NO_IN_JOIN_TIME;
            return;
        }

        eTongBingStage stage = config.getTongBingStage();
        if (stage != eTongBingStage.JoinTime) {
            ret = GameErrorCode.E_TONG_BING_NO_OPERATE_TIME;
            return;
        }

        // 是否有商会
        String unionUid = player.getUserInfo().getUnionUid();
        if (StringUtils.isNullOrEmpty(unionUid)) {
            ret = GameErrorCode.E_TONG_BING_NO_UNION;
            return;
        }
        CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionUid);
        if (unionInfo == null) {
            ret = GameErrorCode.E_TONG_BING_NO_UNION;
            return;
        }

        // 判断职位
        CrossUnionMember unionMember = CrossUnionMgr.getUnionMember(unionUid, player.getUserId());
        if (unionMember == null ||
                (unionMember.getPosition() != eUnionPosition.Master.getValue() && unionMember.getPosition() != eUnionPosition.DeputyMaster.getValue())) {
            ret = GameErrorCode.E_TONG_BING_POSITION_CAN_NO_JOIN;
            return;
        }

        TongBingProto.CrossTongBingJoinReqMsg.Builder builder = TongBingProto.CrossTongBingJoinReqMsg.newBuilder();
        builder.setActivityId(config.getActivityInfo().getActivityId());
        builder.setUnionUid(unionUid);
        builder.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(CrossUnionMgr.parseUnionBaseInfo(unionInfo)));
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_TONG_BING_JOIN, builder, config.getActivityInfo().getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }

        TongBingProto.TongBingJoinRespMsg.Builder respMsg = TongBingProto.TongBingJoinRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(ClientProtocol.U_TONG_BING_JOIN, respMsg);
    }
}
